public class BinaryTree {
    TreeNode root;
    public  void  InsertVal(int val){
        root = insertRec(root,val);
    }
    private  TreeNode insertRec(TreeNode root, int val){
        if (root == null){
            return  new TreeNode(val);
        }
        //递归便利至最小值，添加左节点
        if (val < root.val){
            root.left = insertRec(root.left,val);
            //递归便利至最大值，添右节点
        }else if (val > root.val){
            root.right = insertRec(root.right,val);
        }
        return root;
    }
    //前序 根左右
    public void leftOrder(){
        leftOrderRec(root);
        System.out.println();
    }
   private void leftOrderRec(TreeNode root){
        if (root != null) {
            System.out.println(root.val);
            leftOrderRec(root.left);
            leftOrderRec(root.right);
        }
   }

    //中序 左根右
    public void rightOrder(){
        rightOrderRec(root);
        System.out.println();
    }
    private void rightOrderRec(TreeNode root){
        if (root != null){
            rightOrderRec(root.left);
            System.out.println(root.val);
            rightOrderRec(root.right);
        }
    }

    //后序 左右根
    public void lastOrder(){
        rightOrderRec(root);
        System.out.println();
    }
    private void lastOrderRec(TreeNode root){
        if (root != null) {
            rightOrderRec(root.left);
            rightOrderRec(root.right);
            System.out.println(root.val);
        }
    }

    public static void main(String[] args) {
        BinaryTree binaryTree = new BinaryTree();
        binaryTree.InsertVal(60);
        binaryTree.InsertVal(30);
        binaryTree.InsertVal(20);
        binaryTree.InsertVal(40);
        binaryTree.InsertVal(70);
        binaryTree.InsertVal(80);


        System.out.println("前序");
        binaryTree.leftOrder();
        System.out.println("中序");
        binaryTree.rightOrder();
        System.out.println("后续");
        binaryTree.lastOrder();
    }
}
class TreeNode{
    int val;
    TreeNode left;
    TreeNode right;

    public TreeNode(int val) {
        this.val = val;
        this.left = null;
        this.right = null;
    }
}
